// 运用vuex进行相关的管理操作
import Vue from 'vue'
import Vuex from 'vuex'
import VueXAlong  from 'vuex-along' 

Vue.use(Vuex)
let money
const store = new Vuex.Store({
	// 假数据存储中心
	state: {
		user:null,
		book:null,
	},
	getters:{
		// 根据书籍id获取书籍的信息
		chapterList(state){
			return ( id )=>{
				return state.book[id ].chapterList
			}
		},
		// 获取用户的RMB
		getUserMoney(state){
			return state.user.RMB
		},
		// 获取用户选择的文章类目 
		getUserSelectChapter(state){
			return ({array,Bookid}) => {
				let id = parseInt(Bookid)
			let crueentArry  = array.map(pre => parseInt(pre))
				console.log(crueentArry)
				let crueentArryas = []
				crueentArry.forEach(pre => {
				 crueentArryas.push( state.book[id].chapterList.filter( v => v.number == pre ) ) 
				})
				return crueentArryas.flat()
			}
		},
		// 获取根据id获取用户丁点击的书籍
		getReaderBookById(state){
			return (id)=>{
				console.log(id,)
				return state.user.readbook[0].filter( v => v.id == id )
			}
		},
		// 获取用户
		getUserSelectBook(state){
			return array => {
			let crueentArry  = array.map(pre => parseInt(pre))
				console.log(crueentArry)
				let crueentArryas = []
				crueentArry.forEach(pre => {
				 crueentArryas.push( state.user.readbook[0].filter( v => v.id == pre ) ) 
				})
				return crueentArryas
			}
		}
	},
	mutations: {
		initApp(state,palyload){
			let { user,book } = palyload.palyload
			this.state.user = user  
			this.state.book = book
		},
		ComputedPrice(state,palyload){
			let PirceD =  100
			let { pirceCount } = palyload
			console.log(PirceD-pirceCount )
			
			this.state.user.RMB =  PirceD-pirceCount 
		},
		deleteUserBook(state,palyload){
			let datase = palyload.arry.map( v => parseInt(v) )
			datase.forEach((pre)=>{
				  console.log( this.state.user.readbook[0].filter( v =>  v.id != pre ) ) 
				  let filterdata = this.state.user.readbook[0].filter( v =>  v.id != pre ) 
				  this.state.user.readbook[0] = filterdata
			})
		},
		shopChapter(state,palyload){
			let { selector,bookid } =  palyload
			let id = bookid 

// console.log( id ) 书籍id
console.log(this.state.user.readbook[0][id].chapterList,'-----处理之前')

			let changeBook = []
			
			selector.forEach( (pre) => {
				
			 changeBook.push(this.state.user.readbook[0][id].chapterList.filter( v => v.number == pre.number )[0])  
				
			}) 
			 changeBook.forEach( (pre) => {  pre.isFree = true  })
			
			console.log(this.state.user.readbook[0][id].chapterList,'-----处理之后的')
		
		// 显示的设置
		let newObjes = this.state.user.readbook[0][id].chapterContent[2].isFree = true
			Vue.set( this.state.user.readbook[0][id].chapterContent[2],this.state.user.readbook[0][id].chapterContent[2],newObjes)
			
			
			
		}
	},
	actions: {
	},
	
	//使用vuex-along 中添加的配置
	  plugins: [VueXAlong({ //VueXAlong 引入插件 自定的名字 同上面import 
	  name: 'along',     //存放在localStroage或者sessionStroage 中的名字
	  local: { list: [] , isFilter : true},      //是否存放在local中  false 不存放 如果存放按照下面session的配置配
	  session: { list: [], isFilter: true } //如果值不为false 那么可以传递对象 其中 当isFilter设置为true时， list 数组中的值就会被过滤调,这些值不会存放在seesion或者local中
	  })]
})

export default store
